<template>
  <div class="login">
    <a-modal
      title="全球虚拟物品交易中心"
      v-model="visible"
      :centered="true"
      :bodyStyle="{textAlign:'center',width:'596px',minHeight:'480px'}"
      :afterClose="close"
      :width="596"
      :footer="null"
      :zIndex="1001"
    >
      <div v-if="type == 'login'">
        <div class="tits">
          <img class="logo" src="https://oss.itemplay.com/logozz.png">
        </div>
        
        <a-tabs type="card" class="tabs" :activeKey="loginType" @change="tabChang">
          <a-tab-pane tab="账号密码登录" key="1">
            <a-row type="flex" class="forms">
              <a-col :span="24" class="fitem">
                <a-col :span="6">地区</a-col>
                <a-col :span="16">
                  <a-select v-model="qcode">
                    <a-select-option
                      :value="item.prefix + ',' + index"
                      v-for="(item,index) in qcodeArr"
                      :key="item.id"
                    >+{{item.prefix}} {{item.country}}</a-select-option>
                  </a-select>
                </a-col>
              </a-col>
              <a-col :span="24" class="fitem">
                <a-col :span="6">手机号</a-col>
                <a-col :span="16">
                  <a-input placeholder="请输入手机号" v-model="phone"></a-input>
                </a-col>
              </a-col>
              <a-col :span="24" class="fitem">
                <a-col :span="6">密码</a-col>
                <a-col :span="16">
                  <a-input type="password" placeholder="请输入密码" v-model="pwd"></a-input>
                </a-col>
              </a-col>
            </a-row>
          </a-tab-pane>
          <a-tab-pane tab="手机短信登录" key="2">
            <a-row type="flex" class="forms">
              <a-col :span="24" class="fitem">
                <a-col :span="6">地区</a-col>
                <a-col :span="16">
                  <a-select v-model="qcode">
                    <a-select-option
                      :value="item.prefix + ',' + index"
                      v-for="(item,index) in qcodeArr"
                      :key="item.id"
                    >+{{item.prefix}} {{item.country}}</a-select-option>
                  </a-select>
                </a-col>
              </a-col>
              <a-col :span="24" class="fitem">
                <a-col :span="6">手机号</a-col>
                <a-col :span="16">
                  <a-input placeholder="请输入手机号" v-model="phone"></a-input>
                </a-col>
              </a-col>
              <a-col :span="24" class="fitem">
                <a-col :span="16">
                  <a-input placeholder="请输入短信验证码" v-model="pwd"></a-input>
                </a-col>
                <a-col :span="6">
                  <a-button class="yzbtn" v-if="yzSoft">{{this.yzNum}}</a-button>
                  <a-button class="yzbtn" @click="onYzbtn" v-else>获取验证码</a-button>
                </a-col>
              </a-col>
            </a-row>
          </a-tab-pane>
        </a-tabs>

        <div class="xy">
          登录表示同意
          <a href>《用户协议》</a>和
          <a href>《隐私政策》</a>
        </div>
        <div>
          <a-button class="btn" @click="submit('login')">登录</a-button>
        </div>
        <div class="third">
          <div class="alipay" @click="ailpayLogin()">
            <div class="ali-img"><img src="~/static/ailpay.png"></div>
            <div class="ali-desc">支付宝登录</div>
          </div>
        </div>
        <div class="fd">
          <a-button type="link" @click="link('register')">注册</a-button>
          <a-button type="link" @click="link('reset')">忘记密码</a-button>
        </div>
      </div>
      <div v-if="type == 'register'">
        <div class="tits">
          <img class="logo" src="https://oss.itemplay.com/logozz.png">
        </div>
        <a-row type="flex" class="forms" v-if="registerType">
          <a-col :span="24" class="fitem">
            <a-col :span="6">地区</a-col>
            <a-col :span="16">
              <a-select v-model="qcode">
                <a-select-option
                  :value="item.prefix + ',' + index"
                  v-for="(item,index) in qcodeArr"
                  :key="item.id"
                >+{{item.prefix}} {{item.country}}</a-select-option>
              </a-select>
            </a-col>
          </a-col>
          <a-col :span="24" class="fitem">
            <a-col :span="6">手机号</a-col>
            <a-col :span="16">
              <a-input placeholder="请输入手机号" v-model="phone"></a-input>
            </a-col>
          </a-col>
          <a-col :span="24" class="fitem">
            <a-col :span="16">
              <a-input placeholder="请输入短信验证码" v-model="code"></a-input>
            </a-col>
            <a-col :span="6">
              <a-button class="yzbtn" v-if="yzSoft">{{this.yzNum}}</a-button>
              <a-button class="yzbtn" @click="onYzbtn" v-else>获取验证码</a-button>
            </a-col>
          </a-col>
        </a-row>
        <a-row type="flex" class="formT" v-else>
          <a-col :span="6">设置密码</a-col>
          <a-col :span="16">
            <a-input placeholder="密码（6-16位）" v-model="rpwd"></a-input>
          </a-col>
        </a-row>
        <div class="xy">
          注册表示同意
          <a href>《用户协议》</a>和
          <a href>《隐私政策》</a>
        </div>
        <div>
          <a-button class="btn" @click="onNext('register')" v-if="registerType">下一步</a-button>
          <a-button class="btn" @click="submit('register')" v-else>注册</a-button>
        </div>
        <div class="fd" v-if="registerType">
          <a-button type="link" @click="link('login')">登录</a-button>
        </div>
      </div>
      <div v-if="type == 'reset'">
        <div class="tits">
          <img class="logo" src="https://oss.itemplay.com/logozz.png">
        </div>
        <a-row type="flex" class="forms" v-if="resetType">
          <a-col :span="24" class="fitem">
            <a-col :span="6">地区</a-col>
            <a-col :span="16">
              <a-select v-model="qcode">
                <a-select-option
                  :value="item.prefix + ',' + index"
                  v-for="(item,index) in qcodeArr"
                  :key="item.id"
                >+{{item.prefix}} {{item.country}}</a-select-option>
              </a-select>
            </a-col>
          </a-col>
          <a-col :span="24" class="fitem">
            <a-col :span="6">手机号</a-col>
            <a-col :span="16">
              <a-input placeholder="请输入手机号" v-model="phone"></a-input>
            </a-col>
          </a-col>
          <a-col :span="24" class="fitem">
            <a-col :span="16">
              <a-input placeholder="请输入短信验证码" v-model="code"></a-input>
            </a-col>
            <a-col :span="6">
              <a-button class="yzbtn" v-if="yzSoft">{{this.yzNum}}</a-button>
              <a-button class="yzbtn" @click="onYzbtn" v-else>获取验证码</a-button>
            </a-col>
          </a-col>
        </a-row>
        <a-row type="flex" class="forms" v-else>
          <a-col :span="24" class="fitem">
            <a-col :span="6">输入新密码</a-col>
            <a-col :span="16">
              <a-input placeholder="密码（6-16位）" v-model="pwd2"></a-input>
            </a-col>
          </a-col>
          <a-col :span="24" class="fitem">
            <a-col :span="6">确认新密码</a-col>
            <a-col :span="16">
              <a-input placeholder="密码（6-16位）" v-model="pwd3"></a-input>
            </a-col>
          </a-col>
        </a-row>
        <div class="xy"></div>
        <div>
          <a-button class="btn" @click="onNext('reset')" v-if="resetType">下一步</a-button>
          <a-button class="btn" @click="submit('reset')" v-else>确认</a-button>
        </div>
        <div class="fd" v-if="resetType">
          <a-button type="link" @click="link('login')">登录</a-button>
        </div>
      </div>
    </a-modal>

  </div>
</template>

<script>
import { LOGIN, REGISTER, SAVE_PWD, MSG_DX, GE_PREFIX, GET_INFO ,CHECK_CODE_PWD } from "./server";
export default {
  props: {
    visibles: {
      type: Boolean,
      default: false
    },
    types: {
      type: String,
      default: "login"
    },
    user_id: {
      type: String,
      default: "login"
    }
  },
  data() {
    return {
      qcodeArr: [],
      visible: false,
      phone: '',
      pwd: '',
      loginType: "1",
      t: "login",
      registerType: true,
      code: '',
      rpwd: '',
      resetType: true,
      // phone: "",
      // pwd: "",
      pwd2: "",
      pwd3: "",
      // code: "",
      qcode: "86,0",
      // qcodeArr: [],
      // loginType: "1",
      yzNum: 60,
      yzSoft: false,
      time: "",
    };
  },
  created() {
    GE_PREFIX().then(res => {
      if (!res) return;
      this.qcodeArr = res.data;
    });
  },
  computed: {
    type() {
      return this.t;
    }
  },
  watch: {
    visibles(v) {
      this.t = this.types
      this.visible = v;
    }
  },
  methods: {
    onNext(type) {
      if (!this.validate(["phone", "code"])) return;
      CHECK_CODE_PWD({
        phone: "+" + this.qcode.split(",")[0] + this.phone,
        code: this.code,
      }).then(res => {
        if (res != null) {
          this.registerType = false
          this.resetType = false
        } else {
          this.$message.error('验证码错误');
        }
      });
    },
    onYzbtn() {
      if (!this.validate(["phone"])) return;
      this.yzSoft = true;
      this.$nextTick(() => {
        this.time = setInterval(() => {
          if (this.yzNum == 1) {
            clearInterval(this.time);
            this.yzSoft = false;
            this.yzNum = 60;
          }
          this.yzNum--;
        }, 1000);
      });
      MSG_DX({ phone: "+" + this.qcode.split(",")[0] + this.phone }).then(
        res => {
          if (!res) return;
          this.$message.success(res.msg);
        }
      );
    },
    tabChang(v) {
      this.pwd = ''
      this.loginType = v;
    },
    submit(n) {
      switch (n) {
        case "login":
          if (!this.validate(["phone", "pwd"])) return;
          LOGIN({
            phone: "+" + this.qcode.split(",")[0] + this.phone,
            // phone: '123',
            pwd: this.pwd,
            type: this.loginType
          }).then(res => {
            if (res.code != 200) {
              this.$message.error(res.msg);
            } else {
              this.$token.setCookie("member_id", res.data.member_id);
              this.$token.setCookie("phone", res.data.phone);
              this.$store.commit("cookie", {
                member_id: res.data.member_id,
                phone: res.data.phone
              });
              this.$store.commit("login", true)
              this.$notification.open({
                message: '登录成功！',
                description:
                  '欢迎使用全球虚拟物品交易中心',
                icon: <a-icon type="check-circle" style="color: #52c41a" />,
              });
              this.close();
            }
          });
          break;
        case "register":
          let user_id
          if (this.user_id) {
            user_id = this.user_id
          }
          if (!this.validate(["rpwd"])) return;
          REGISTER({
            phone: "+" + this.qcode.split(",")[0] + this.phone,
            pwd: this.rpwd,
            type: 2,
            user_id
          }).then(res => {
            if (res.code != 200) {
              this.$message.error(res.msg);
              return;
            } else {
              this.$message.success(res.msg);
              this.link("login");
            }
          });
          break;
        case "reset":
          if (!this.validate(["pwd2", "pwd3"])) return;
          SAVE_PWD({
            phone: "+" + this.qcode.split(",")[0] + this.phone,
            pwd: this.pwd3
          }).then(res => {
            if (res.code != 200) {
              this.$message.error(res.msg);
              return;
            } else {
              this.$message.success(res.msg);
              this.link("login");
            }
          });
          break;
      }
    },
    validate(arr) {
      if (arr.includes("phone") && !this.phone) {
        this.$message.error("手机号不能为空！");
        return false;
      }
      if (arr.includes("pwd") && !this.pwd) {
        if (this.loginType == 1) {
          this.$message.error("密码不能为空！");
        } else {
          this.$message.error("验证码不能为空！");
        }
        return false;
      }
      if (arr.includes("rpwd")) {
        console.log(this.rpwd.length, 387)
        if (this.rpwd.length > 5 && this.rpwd.length < 17) {
          return true
        } else {
          this.$message.error("密码长度有误");
          return false
        }
      }
      if (arr.includes("pwd2") && arr.includes("pwd3")) {
        if (this.pwd2.length > 5 && this.pwd2.length < 17) {
          if (this.pwd2 != this.pwd3) {
            this.$message.error("两次输入密码不同！");
            return false
          } else {
            return true
          }
        } else {
          this.$message.error("密码长度有误");
          return false
        }
      }
      if (arr.includes("code") && !this.phone) {
        this.$message.error("验证码不能为空！");
        return false;
      }
      return true;
    },
    link(e) {
      this.loginType = "1"
      this.t = e;
      this.registerType = true;
      this.phone = "";
      this.pwd = "";
      this.code = "";
      this.rpwd = ""
      this.yzSoft = false;
      this.yzNum = 60;
      this.qcode = "86,0";
      this.resetType = true;
      this.pwd2 = "";
      this.pwd3 = "";
      clearInterval(this.time);
    },
    close() {
      this.loginType = "1"
      this.phone = "";
      this.pwd = "";
      this.rpwd = ""
      this.yzSoft = false;
      this.yzNum = 60;
      this.registerType = true;
      this.resetType = true;
      this.pwd2 = "";
      this.pwd3 = "";
      this.code = "";
      clearInterval(this.time);
      this.$emit("close");
      this.qcode = "86,0";
    },
    ailpayLogin() {
      GET_INFO().then(res => {
        window.location.href= res.data
      })
    }
  }
};
</script>

<style lang="less" scoped>
.tits {
  margin: 10px 0 50px 0;
  font-size: 21px;
  color: #333333;
  .logo {
    width: 162px;
    height: 85px;
  }
}
.tabs {
  width: 320px;
  display: inline-block;
  .forms {
    border-top: 0;
  }
  &/deep/.ant-tabs-bar {
    margin: 0;
    .ant-tabs-nav {
      width: 100%;
      .ant-tabs-tab {
        margin: 0;
        width: 50%;
      }
    }
  }
}
.forms {
  width: 320px;
  border: solid 1px #dadada;
  display: inline-block;
  .fitem {
    height: 46px;
    line-height: 46px;
    border-bottom: solid 1px #dadada;
    &:last-child {
      border: 0;
    }
    div:nth-child(1) {
      margin-right: 10px;
    }
    img {
      width: 68px;
      height: 24px;
    }
    .yzbtn {
      width: 82px;
      height: 30px;
      background-color: #0084ff;
      color: #fff;
      border-color: #fff;
      padding: 0;
    }
    &/deep/.ant-select {
      width: 100%;
      .ant-select-selection {
        border: 0;
      }
    }
  }
  input {
    width: 100%;
    border: 0;
    border-color: #fff;
  }
}
.formT {
  width: 320px;
  height: 46px;
  line-height: 46px;
  border: solid 1px #dadada;
  display: inline-block;
  div:nth-child(1) {
    margin-right: 10px;
  }
  input {
    width: 100%;
    border: 0;
    border-color: #fff;
  }
}
.xy {
  margin: 16px 0;
  padding-right: 44px;
  a {
    color: #999999;
  }
}
.third {
  display: flex;
  padding-bottom: 10px;
  .alipay {
    display: flex;
    align-items: center;
    cursor: pointer;
    margin-left: 114px;
    .ali-img {
      width: 16px;
      height: 16px;
      display: flex;
      margin-right: 2px;
      img {
        margin: 0;
      }
    }
    .ali-desc {
      font-size: 12px;
    }
  }
}
.btn {
  width: 320px;
  height: 44px;
  background-color: #0084ff !important;
  color: #fff !important;
  border-color: #fff !important;
  margin-bottom: 12px;
}
.fd {
  button:nth-child(1) {
    color: #0084ff !important;
  }
  button:nth-child(2) {
    color: #999999 !important;
  }
}

</style>